package cn.dujc.superkid.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import cn.dujc.core.adapter.BaseAdapter;
import cn.dujc.core.adapter.BaseViewHolder;
import cn.dujc.core.adapter.entity.IExpandable;
import cn.dujc.core.adapter.util.MultiTypeDelegate;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.activities.ExaminationActivity;
import cn.dujc.superkid.activities.MyTestResultsActivity;
import cn.dujc.superkid.bean.ExamineListResult;
import cn.dujc.superkid.bean.Main2Entity;
import cn.dujc.superkid.bean.VideoListResult;

public class ExamineListAdapter extends BaseAdapter<IExpandable> {

    public ExamineListAdapter(@Nullable List<IExpandable> data) {
        super(data);
        //多种类型的数据，必须实现以下全部代码
        final MultiTypeDelegate<IExpandable> delegate = new MultiTypeDelegate<IExpandable>() {
            @Override
            protected int getItemType(IExpandable o) {
                return o.getLevel();//因为实体设置了不同的level，所以这边返回level就行
            }
        };
        delegate.registerItemType(0, R.layout.item_level0);//即为前面实体类的那个level
        delegate.registerItemType(1, R.layout.item_level1);//即为前面实体类的那个level
        setMultiTypeDelegate(delegate);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final IExpandable item) {
        if (helper.getItemViewType() == 0) {//即为上面的0或1
            if (((Main2Entity)item).getTitle().equals("一年级")){
                helper.setGone(R.id.v_kong,false);
            }else {
                helper.setGone(R.id.v_kong,true);
            }

            if (((Main2Entity)item).getTitle().equals("一年级")||((Main2Entity)item).getTitle().equals("四年级")){
                helper.setImageResource(R.id.iv_grade,R.mipmap.yi_nian_ji);
                helper.setText(R.id.tv_grade,((Main2Entity)item).getTitle());
            }else if (((Main2Entity)item).getTitle().equals("二年级")||((Main2Entity)item).getTitle().equals("五年级")){
                helper.setImageResource(R.id.iv_grade,R.mipmap.er_nian_ji);
                helper.setText(R.id.tv_grade,((Main2Entity)item).getTitle());
            }else if(((Main2Entity)item).getTitle().equals("三年级")||((Main2Entity)item).getTitle().equals("六年级")){
                helper.setImageResource(R.id.iv_grade,R.mipmap.san_nian_ji);
                helper.setText(R.id.tv_grade,((Main2Entity)item).getTitle());
            }
            helper.itemView.setOnClickListener(new View.OnClickListener() {//最外层要实现展开与关闭，所以需要设置点击
                @Override
                public void onClick(View v) {
                    if (item.isExpanded()) {
                        collapse(helper.getAdapterPosition());
                    }
                    else {
                        expand(helper.getAdapterPosition());
                    }
                }
            }
            );
            if (item.isExpanded()) {
                helper.setImageResource(R.id.iv_grade_select,R.mipmap.icon_open);
            }
            else {
                helper.setImageResource(R.id.iv_grade_select,R.mipmap.xiang_you_xiao_jian_tou);
            }
        } else {//二级的文字设置
            helper.setText(R.id.tv_grade_title,((Main2Entity.InnerEntity)item).getChild().getEx_title());
            helper.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    starter()
                            .with(ExaminationActivity.EXTRA_MYTEST,((Main2Entity.InnerEntity)item).getChild().getId())
                            .go(ExaminationActivity.class);
                }
            });
        }
    }

}
